#!/bin/bash
if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash build.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1

create_dir()
{
    local path=$1
    [[ "$path" =~ .*/$ ]] && path=${path%/*}
    if [[ ! -d "$path" && ! -f "$path" ]]; then mkdir -p "$path"; else path=$path$(date "+%y%m%d%H%M%S") && mkdir -p "$path"; fi
    echo "$path"
}

buildpath=$(create_dir "$1")
installpath=$(create_dir "$2")

yum install -y m4 environment-modules systemd-devel
source /etc/profile
module purge

set -e
yum install -y sqlite-devel zlib zlib-devel readline-devel openssl openssl-devel \
curl-devel tk tk-devel gdbm-devel xz-devel bzip2-devel gcc-gfortran libXt-devel \
pcre-devel cmake cmake-gui lua lua-devel tcl tcl-devel gtest gtest-devel 
wget -t 10 -c -P $buildpath https://repo.huaweicloud.com/openeuler/openEuler-20.03-LTS/EPOL/aarch64/Packages/libomp-7.0.1-2.oe1.aarch64.rpm
rpm -ivh --replacepkgs $buildpath/libomp-7.0.1-2.oe1.aarch64.rpm
rm -rf $buildpath/libomp-7.0.1-2.oe1.aarch64.rpm

dep_dir="dependency"
bash $dep_dir/bisheng-compiler-2.1.0.sh "$buildpath" "$installpath"
module use "$installpath"/bisheng-compiler-2.1.0
module load "$installpath"/bisheng-compiler-2.1.0/bisheng_modulefiles

bash $dep_dir/Python-3.9.0.sh "$buildpath" "$installpath"
module use "$installpath"/Python-3.9.0
module load "$installpath"/Python-3.9.0/Python_modulefiles
bash $dep_dir/R-4.1.0.sh "$buildpath" "$installpath"
module use "$installpath"/R-4.1.0
module load "$installpath"/R-4.1.0/R_modulefiles

bash $dep_dir/ITK-5.2.1.sh "$buildpath" "$installpath"
module use "$installpath"/ITK-5.2.1
module load "$installpath"/ITK-5.2.1/ITK_modulefiles

bash $dep_dir/SWIG-4.0.2.sh "$buildpath" "$installpath"
module use "$installpath"/SWIG-4.0.2
module load "$installpath"/SWIG-4.0.2/SWIG_modulefiles

bash SimpleITK-2.1.1.1.sh "$buildpath" "$installpath"
module use "$installpath"/SimpleITK-2.1.1.1
module load "$installpath"/SimpleITK-2.1.1.1/SimpleITK_modulefiles

mkdir -p pkg/SimpleITK-2.1.1.1-hpc && cp -r "$installpath"/* pkg/SimpleITK-2.1.1.1-hpc/
cat>"pkg/SimpleITK-2.1.1.1-hpc/run.sh"<<EOF
#!/bin/bash
sudo yum install -y m4 environment-modules systemd-devel csh time patch libatomic perl-Data-Dumper \\
autoconf automake libtool numactl binutils valgrind flex wget tar libatomic gcc-c++ git 
sudo yum install -y sqlite-devel zlib zlib-devel readline-devel openssl openssl-devel \\
curl-devel tk tk-devel gdbm-devel xz-devel bzip2-devel gcc-gfortran libXt-devel \\
pcre-devel cmake cmake-gui lua lua-devel tcl tcl-devel gtest gtest-devel
wget -t 10 -c https://repo.huaweicloud.com/openeuler/openEuler-20.03-LTS/EPOL/aarch64/Packages/libomp-7.0.1-2.oe1.aarch64.rpm
rpm -ivh --replacepkgs libomp-7.0.1-2.oe1.aarch64.rpm
rm -rf libomp-7.0.1-2.oe1.aarch64.rpm
source /etc/profile &> /dev/null 
current_dir="\$(cd "\$(dirname "\${BASH_SOURCE[0]}")" && pwd -P)"
for f in "\${current_dir}"/*; do
  if [[ -d "\${f}" && -f "\$(ls "\${f}"/*modulefiles 2>&1)" ]]; then
    module use "\${f}" && module load "\${f}"/*modulefiles
  fi
done

libnn="\\\${libnn}"
libnn_fallback="\\\${libnn_fallback}"
sed -i "4cR_HOME_DIR=\$R_PATH/lib/R" \$R_PATH/bin/R
sed -i "5cif test "\${R_HOME_DIR}" = "\$R_PATH/lib/R"; then
" \$R_PATH/bin/R
sed -i "19c     if [ -x "\$R_PATH/\${libnn}/R/bin/exec/R" ]; then" \$R_PATH/bin/R
sed -i "20c        R_HOME_DIR="\$R_PATH/\${libnn}/R"" \$R_PATH/bin/R
sed -i "21c     elif [ -x "\$R_PATH/\${libnn_fallback}/R/bin/exec/R" ]; then" \$R_PATH/bin/R
sed -i "22c        R_HOME_DIR="\$R_PATH/\${libnn_fallback}/R"" \$R_PATH/bin/R
sed -i "35cR_SHARE_DIR=\$R_PATH/lib/R/share" \$R_PATH/bin/R
sed -i "37cR_INCLUDE_DIR=\$R_PATH/lib/R/include" \$R_PATH/bin/R
sed -i "39cR_DOC_DIR=\$R_PATH/lib/R/doc" \$R_PATH/bin/R

echo -e "\033[1;32;1mSimpleITK environment initialization completed.\033[0m"

EOF
cd pkg/ && tar zcvf SimpleITK-2.1.1.1-hpc.tar.gz SimpleITK-2.1.1.1-hpc

rm -rf "$buildpath" "$installpath"
